//
//  favoriteCitysViewController.m
//  weather
//
//  Created by Mfk on 13-1-4.
//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.
//

#import "favoriteCitysViewController.h"
#import "todayViewController.h"
@interface favoriteCitysViewController ()

@end

@implementation favoriteCitysViewController
@synthesize cityList;
@synthesize table;
@synthesize filePath;
- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"%d",[self.cityList count]);
    UIImageView *tableBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"city"]];
    [self.table setBackgroundView:tableBg];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
 
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
     self.navigationItem.rightBarButtonItem = self.editButtonItem;
}

- (void)viewDidUnload
{
    [self setTable:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    // Return the number of rows in the section.
    return [self.cityList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if(cell==nil)
    {
        cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    // Configure the cell...
    cell.textLabel.text=[self.cityList objectAtIndex:[indexPath row]];
    cell.textLabel.textColor=[UIColor whiteColor];
    return cell;
}



- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}


// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //写入文件
        UINavigationController *nav=self.navigationController;
        [nav popViewControllerAnimated:YES];
        NSArray *allControllers=nav.viewControllers;
        todayViewController *today=[allControllers lastObject];
        filePath=[today dataFilePath];
        [self.cityList removeObjectAtIndex:[indexPath row]];
        BOOL isdd=[self.cityList writeToFile:filePath atomically:YES];
        if(isdd)
            NSLog(@"true");

        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        [self.table reloadData];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/


        


#pragma mark - Table view delegate
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UINavigationController *nav=self.navigationController;
    [nav popViewControllerAnimated:YES];
    NSArray *allControllers=nav.viewControllers;
    todayViewController *today=[allControllers lastObject];
    NSString * str=[self.cityList objectAtIndex:[indexPath row]];
    today.cityname=str;
    filePath=[today dataFilePath];
    [today reload];
    
    
    
    
}   
    //UIStoryboard *storyboard=[UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    //todayViewController *next=[storyboard  instantiateViewControllerWithIdentifier:@"today"];
    //next.cityDict=[next getWeatherInfo:[self.cityList objectAtIndex:[indexPath row]]];
    
    
    //[self.navigationController pushViewController:next animated:YES];


@end
